package com.hundsun.s3.platform.backstage.action.report;

import com.hundsun.jresplus.common.util.StringUtil;
import com.hundsun.s3.common.dto.DataObj;
import com.hundsun.s3.common.dto.PageBean;
import com.hundsun.s3.common.dto.PieDto;
import com.hundsun.s3.common.dto.StackedLine;
import com.hundsun.s3.platform.backstage.common.util.ChartUtils;
import com.hundsun.s3.platform.service.CommonService;
import com.hundsun.s3.platform.service.OrderReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * com.hundsun.s3.platform.backstage.action.order
 * @Description:
 * @Author: yangjun23614
 * @Date: Created in 16:23 2018/2/26
 * V1.0 16:23 2018/2/26
 */
@Controller
@RequestMapping("/back/report")
public class OrderAction {
    @Autowired
    private OrderReportService orderReportService;
    @Autowired
    private CommonService commonService;
    
    /**
     * @Description: 查询所有工单（分页）
     * @param: [pageSize, pageNo]
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @Author: yangjun23614
     * @Date: 16:43 2018/2/26
     * V1.0
     */
    @RequestMapping("/orderQry.json")
    public @ResponseBody Map<String,Object> orderQuery(int pageSize,int pageNo){
        PageBean pageBean = new PageBean();
        pageBean.setPNoPSize(pageNo, pageSize);
        Map map = orderReportService.getOrdersByPage(pageBean);
        Map<String,Object> rs = new HashMap<>();
        rs.put("rows",(List<Order>)(map.get("list")));
        rs.put("total",((PageBean)map.get("page")).getCount());
        return rs;
    }

    /**
     * @Description: 根据条件查询，根据传入的参数是否为空值进行不同的查询。（分页）
     * @param: [pageSize,pageNo,staff_id,server_start_time,server_end_time]
     * @return: order结果集 及 查询到的总数
     * @Author: yangjun23614
     * @Date: 13:53 2018/2/28
     * V1.0 
     */
    @RequestMapping("/orderQryByIdTime.json")
    public @ResponseBody Map<String,Object> orderQueryByIdTime(int pageSize, int pageNo, String staff_id,
                                                 String server_start_time, String server_end_time) {
        if (StringUtil.isEmpty(staff_id) && (StringUtil.isEmpty(server_start_time) || StringUtil.isEmpty(server_end_time))) {
            return this.orderQuery(pageSize, pageNo);
        } else if(StringUtil.isEmpty(staff_id) && StringUtil.isNotEmpty(server_start_time) && StringUtil.isNotEmpty(server_end_time)){
            PageBean pageBean = new PageBean();
            pageBean.setPNoPSize(pageNo, pageSize);
            if (StringUtil.isEmpty(staff_id)) {
                staff_id = "0";
            }
            Long startTime = Long.parseLong(server_start_time)*1000000;
            Long endTime = Long.parseLong(server_end_time)*1000000;
            Map map = orderReportService.getOrdersByTime(startTime, endTime, pageBean);
            Map<String, Object> rs = new HashMap<>();
            rs.put("rows", (List<Order>) (map.get("list")));
            rs.put("total", ((PageBean) map.get("page")).getCount());
            return rs;
        } else {
            //staffid不为空但时间为空和全都不为空，两种都能查询
            //判断为空
            PageBean pageBean = new PageBean();
            pageBean.setPNoPSize(pageNo, pageSize);
            if (StringUtil.isEmpty(staff_id)) {
                staff_id = "0";
            }
            if(StringUtil.isEmpty(server_start_time)){
                server_start_time ="0";
            }
            if(StringUtil.isEmpty(server_end_time)){
                server_end_time ="0";
            }
            Long startTime = Long.parseLong(server_start_time)*1000000;
            Long endTime = Long.parseLong(server_end_time)*1000000;
            Map map = orderReportService.getOrdersByIdTime(staff_id, startTime, endTime, pageBean);
            Map<String, Object> rs = new HashMap<>();
            rs.put("rows", (List<Order>) (map.get("list")));
            rs.put("total", ((PageBean) map.get("page")).getCount());
            return rs;
        }
    }

    /**
     * @Description: 工单年份折线图统计数据(当前年份)
     * @param: year
     * @return: 当年各月份的数据及图标信息
     * @Author: yangjun23614
     * @Date: 10:39 2018/3/1
     * V1.0 
     */
    @RequestMapping("/getOrderStackedData.json")
    public @ResponseBody
    StackedLine getOrderStackedData(){
        StackedLine stackedLine = new StackedLine();
        String[] legendData = {Integer.toString(ChartUtils.getThisYear())};
        String[] xData = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
        List<DataObj> list = new ArrayList<DataObj>();
        List<int[]> datas = new ArrayList<int[]>();
        int[] data = orderReportService.getOrderStackedDataByYear(ChartUtils.getThisYear());
        datas.add(data);
        int i = 0;
        ChartUtils.setStackedLineYData(legendData,datas,list);
        stackedLine.setxData(xData);
        stackedLine.setyData(list);
        return stackedLine;
    }

    /**
     * @Description: 工单年份折线图统计数据(多年份)
     * @param: yearsCode是前端传来的code,是从0开始的数字，应该加2018
     * @return: 每年各月份的数据及图标信息
     * @Author: yangjun23614
     * @Date: 16:47 2018/3/1
     * V1.0 
     */
    @RequestMapping("/getOrderStackedDataByYear.json")
    public @ResponseBody
    StackedLine getOrderStackedDataByYear(String yearsCode){
        String[] s = yearsCode.split(",");
        int[] yearsInteger = new int[s.length];
        String[] legendData = new String[s.length];
        for (int i = 0; i < s.length; i++) {
            yearsInteger[i] = Integer.parseInt(s[i])+2016;
            legendData[i] = Integer.toString(yearsInteger[i]);
        }
        StackedLine stackedLine = new StackedLine();
        String[] xData = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
        List<DataObj> list = new ArrayList<DataObj>();
        List<int[]> datas = new ArrayList<int[]>();
        for(int i = 0; i < yearsInteger.length; i++){
            int[] data = orderReportService.getOrderStackedDataByYear(yearsInteger[i]);
            datas.add(data);
        }
        ChartUtils.setStackedLineYData(legendData,datas,list);
        stackedLine.setxData(xData);
        stackedLine.setyData(list);
        return stackedLine;
    }

    /**
     * @Description: 根据月查所有
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 16:51 2018/3/6
     * V1.0
     */
    @RequestMapping("/getOrderStackedDataByMonth.json")
    public @ResponseBody
    StackedLine getOrderStackedDataByMonth(String find_year_month){
        String[] legendData = new String[1];
        if (StringUtil.isEmpty(find_year_month)){
            find_year_month = Integer.toString(ChartUtils.getThisYearAndMonth());
            legendData[0] = find_year_month;
        } else {
            legendData[0] = find_year_month;
        }
        StackedLine stackedLine = new StackedLine();
        String[] xData = new String[31];
        for(int i = 0; i < 31; i++) {
            xData[i] = ((Integer)(i+1)).toString();
        }
        List<DataObj> list = new ArrayList<DataObj>();
        List<int[]> datas = new ArrayList<int[]>();
//        int[] data = orderReportService.getOrderStackedDataByMonth(Integer.parseInt(find_year_month));
        Map<String,Object> params = new HashMap<>();
        params.put("find_year_month",Integer.parseInt(find_year_month));
        int[] data = orderReportService.getOrderStackedDataByParams(params,"400021");
        datas.add(data);
        ChartUtils.setStackedLineYData(legendData,datas,list);
        stackedLine.setxData(xData);
        stackedLine.setyData(list);
        return stackedLine;
    }

    /**
     * @Description: 根据月、部门查所有
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 16:51 2018/3/6
     * V1.0
     */
    @RequestMapping("/getOrderStackedDataByMonthDept.json")
    public @ResponseBody
    StackedLine getOrderStackedDataByMonthDept(int deptNum, String find_year_month){
        String[] legendData = new String[1];
        if (StringUtil.isEmpty(find_year_month)){
            find_year_month = Integer.toString(ChartUtils.getThisYearAndMonth());
            legendData[0] = find_year_month;
        } else {
            legendData[0] = find_year_month;
        }
        StackedLine stackedLine = new StackedLine();
        String[] xData = new String[31];
        for(int i = 0; i < 31; i++) {
            xData[i] = ((Integer)(i+1)).toString();
        }
        List<DataObj> list = new ArrayList<DataObj>();
        List<int[]> datas = new ArrayList<int[]>();
        Map<String,Object> params = new HashMap<>();
        params.put("deptNum",deptNum);
        params.put("find_year_month",Integer.parseInt(find_year_month));
        params.put("deptGrade",commonService.getDeptGradeByDeptNo(deptNum));
;       int[] data = orderReportService.getOrderStackedDataByParams(params,"400022");
        datas.add(data);
        ChartUtils.setStackedLineYData(legendData,datas,list);
        stackedLine.setxData(xData);
        stackedLine.setyData(list);
        return stackedLine;
    }

    /**
     * @Description: 获取历史年份
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 17:35 2018/3/6
     * V1.0
     */
    @RequestMapping(value = "/getPastYears.json")
    public @ResponseBody List<Map<String,String>> getPastYears(){
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> map;
        int thisYear  = ChartUtils.getThisYear();
        int j = 0;
        for(int i = 2016; i <= thisYear; i++){
            map = new HashMap<>();
            map.put("text",Integer.toString(i));
            map.put("code",Integer.toString(j));
            j++;
            list.add(map);
        }
        return list;
    }

    /**
     * @Description: 根据年份获取反馈等级数据
     * @param: [String]
     * @return:
     * @Author: yangjun23614
     * @Date: 15:55 2018/3/5
     * V1.0
     */
    @RequestMapping(value = "/getFeedBackPieDataByYear.json")
    public @ResponseBody List getPieDataByYear(String year) {
        if(StringUtil.isEmpty(year)){
            year = Integer.toString(ChartUtils.getThisYear());
        }
        String[] legendData = {"1","2","3","4","5"};
        List<PieDto> list = new ArrayList<PieDto>();
        Map<String,Object> params = new HashMap<>();
        params.put("find_year",Integer.parseInt(year));
        int[] data = orderReportService.getFeedBackPieDataByData(params,"400018");
        ChartUtils.setPieData(legendData,list,data);
        return list;
    }

    /**
     * @Description: 按月查询
     * @param: 
     * @return:  
     * @Author: yangjun23614
     * @Date: 10:12 2018/3/8
     * V1.0 
     */
    @RequestMapping(value = "getFeedBackPieDataByMonth.json")
    public @ResponseBody List getFeedBackPieDataByMonth(String find_year_month){
        if(StringUtil.isEmpty(find_year_month)){
            find_year_month = Integer.toString(ChartUtils.getThisYearAndMonth());
        }
        Map<String,Object> result = new HashMap<String,Object>();
        String[] legendData = {"1","2","3","4","5"};
        List<PieDto> list = new ArrayList<PieDto>();
        Map<String,Object> params = new HashMap<>();
        params.put("find_year_month",Integer.parseInt(find_year_month));
        int[] data = orderReportService.getFeedBackPieDataByData(params,"400019");
        ChartUtils.setPieData(legendData,list,data);
        return list;
    }

}
